<?php

namespace platform;

use app\datacenter\model\Shop;
use think\facade\Log;
use wechat\WsOfficialAccount;

/**
 * 绿葆公众号平台
 */
class LvBaoPlatform
{
    //默认取号地址
    public string $post_url;
    public string $appId;
    public string $appSecret;
    public string $siteKey;

    /**
     * 构造方法
     */
    public function __construct()
    {
        //环境加载配置
        if (env('ENVIRONMENT') == 'dev') {
            //测试环境
            $this->post_url = "http://t.api.ngrok.greenboniot.cn";
            $this->appId = 'gb6392jv9iq6x9';
            $this->appSecret = 'D6QI1bj4L2fU6C77Nr2L1X34sJKScH';
            $this->siteKey = 'PS9e7s51E4sr4J9';
        } else {
            //正式环境
            $this->post_url = "https://api.greenboniot.cn";
            $this->appId = 'gb7887cp5a9417';
            $this->appSecret = '1x2GVD8fw0397ho7P7CTq7j8Gky9su';
            $this->siteKey = 'PSLq27F2lV61wFa';
        }
    }

    /**
     * 公众号取号接口
     * @param string $uuid
     * @param string $nickName
     * @param int $gender
     * @param string $mac_id
     * @return array
     */
    public function queryInterface(string $uuid, string $nickName, int $gender, string $mac_id): array
    {
        $OfficialAccount = new WsOfficialAccount();
        $newsTemplate = $OfficialAccount->newsTemplate;
        $messageTemplate = [
            "keyword" => "",
            "msgType" => "news",
            "title" => $newsTemplate['title'],
            "description" => $newsTemplate['description'],
            "url" => $newsTemplate['url'],
            "picUrl" => $newsTemplate['image']
        ];
        //获取当前设备所在位置Code
        $RegionCode = $this->getRegionCode($mac_id);
        //请求参数
        $post_data = [
            'appId' => $this->appId,
            'timestamp' => getMillisecond(),
            'nonce' => uuid(),
            "siteKey" => $this->siteKey,
            "provinceCode" => $RegionCode['provinceCode'],
            "cityCode" => $RegionCode['cityCode'],
            "districtCode" => $RegionCode['districtCode'],
            "deviceType" => 201,
            "sceneCode" => [
                101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115
            ],
            'authOpenId' => $uuid,
            'nickName' => $nickName,
            'gender' => $gender,
            //推送消息
            'replyConfig' => [
                "replyType" => [1, 5],
                //消息模板集合
                "messageTemplate" => [
                    array_merge(["selectReplyType" => 1,], $messageTemplate),
                    array_merge(["selectReplyType" => 5,], $messageTemplate),
                ]
            ],
            'phoneType' => 3,
        ];
        //安全签名规则
        $signature = secureSignature($post_data, $this->appSecret);
        $params_ready = [
            'json' => array_merge($post_data, ['signature' => $signature])
        ];
        $url = $this->post_url . '/manage/api/v1/bpopen/get/qrcode';
        $result = httpPost($url, $params_ready);
        Log::write("请求=".json_encode($post_data)."===返回".json_encode($result),'绿葆取号请求');
        $content = json_decode($result['content'], true);

        //判断结果
        if (isset($content['code']) and $content['code'] == 0 ) {
            if (isset($content['data']['wxAppType']) && in_array($content['data']['wxAppType'], [1,2, 3])) {
                return $content['data'];
            }
        }
        return [];
    }

    /**
     * 通过设备ID获取所在地区编码
     * @param string $mac_id
     * @return array
     */
    private function getRegionCode(string $mac_id): array
    {
        $data['provinceCode'] = '0';
        $data['cityCode'] = '0';
        $data['districtCode'] = '0';
        //获取当前设备的shop信息
        $ShopInfo = Shop::getShopInfo($mac_id);
        if ($ShopInfo) {
            $data['provinceCode'] = $ShopInfo['province_id'];
            $data['cityCode'] = $ShopInfo['city_id'];
            $data['districtCode'] = $ShopInfo['region_id'];
        }
        return $data;
    }

}